<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="市场名称" prop="marketplaceId">
        <el-select v-model="queryParams.marketplaceId" placeholder="请选择市场名称" clearable size="small">
          <el-option
            v-for="dict in marketplaceIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地区名称" prop="regionCode">
        <el-select v-model="queryParams.regionCode" placeholder="请选择地区名称" clearable size="small">
          <el-option
            v-for="dict in regionCodeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态" prop="useStatus">
        <el-select v-model="queryParams.useStatus" placeholder="请选择使用状态" clearable size="small">
          <el-option
            v-for="dict in useStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['amz:marketplace:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['amz:marketplace:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['amz:marketplace:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="handleSyncCurrencyRate"
          v-hasPermi="['amz:marketplace:syncCurrencyRate']"
        >同步汇率</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['amz:marketplace:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="marketplaceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50"/>
      <el-table-column label="市场名称" width="80" align="center" prop="marketplaceName" />
      <el-table-column label="国家代码" width="80" align="center" prop="countryCode" />
      <el-table-column label="地区名称" width="80" align="center" prop="regionCode" :formatter="regionCodeFormat" />
      <el-table-column label="语言代码" width="80" align="center" prop="languageCode" />
      <el-table-column label="货币代码" width="80" align="center" prop="currencyCode" />
      <el-table-column label="货币汇率" width="80" align="center" prop="currencyRate" />
      <el-table-column label="授权URL域名" align="center" prop="oauthUri" :show-overflow-tooltip="true" />
      <el-table-column label="销售端点网址" align="center" prop="apiEndpointUri" :show-overflow-tooltip="true" />
      <el-table-column label="LWA授权证书" align="center" prop="lwaEndpointUri" :show-overflow-tooltip="true" />
      <el-table-column label="AWS区域" width="100" align="center" prop="awsRegion" />
      <el-table-column label="使用状态" width="80" align="center" prop="useStatus" :formatter="useStatusFormat" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" width="180" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['amz:marketplace:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['amz:marketplace:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改站点维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="市场名称" prop="marketplaceId">
          <el-select v-model="form.marketplaceId" placeholder="请选择市场名称">
            <el-option
              v-for="dict in marketplaceIdOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市场地址" prop="marketplaceUri">
          <el-input v-model="form.marketplaceUri" placeholder="请输入市场地址" />
        </el-form-item>
        <el-form-item label="国家代码" prop="countryCode">
          <el-input v-model="form.countryCode" placeholder="请输入国家代码" />
        </el-form-item>
        <el-form-item label="地区名称" prop="regionCode">
          <el-select v-model="form.regionCode" placeholder="请选择地区名称">
            <el-option
              v-for="dict in regionCodeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="语言代码" prop="languageCode">
          <el-input v-model="form.languageCode" placeholder="请输入语言代码" />
        </el-form-item>
        <el-form-item label="货币代码" prop="currencyCode">
          <el-input v-model="form.currencyCode" placeholder="请输入货币代码" />
        </el-form-item>
        <el-form-item label="货币汇率" prop="currencyRate">
          <el-input v-model="form.currencyRate" placeholder="请输入货币汇率" />
        </el-form-item>
        <el-form-item label="授权URL域名" prop="oauthUri">
          <el-input v-model="form.oauthUri" placeholder="请输入授权URL域名" />
        </el-form-item>
        <el-form-item label="销售端点网址" prop="apiEndpointUri">
          <el-input v-model="form.apiEndpointUri" placeholder="请输入销售端点网址" />
        </el-form-item>
        <el-form-item label="LWA授权证书" prop="lwaEndpointUri">
          <el-input v-model="form.lwaEndpointUri" placeholder="请输入LWA授权证书" />
        </el-form-item>
        <el-form-item label="AWS区域" prop="awsRegion">
          <el-input v-model="form.awsRegion" placeholder="请输入AWS区域" />
        </el-form-item>
        <el-form-item label="使用状态" prop="useStatus">
          <el-select v-model="form.useStatus" placeholder="请选择使用状态">
            <el-option
              v-for="dict in useStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMarketplace,
  getMarketplace,
  delMarketplace,
  addMarketplace,
  updateMarketplace,
  exportMarketplace,
  syncCurrencyRate
} from "@/api/amz/marketplace";

export default {
  name: "Marketplace",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站点维护表格数据
      marketplaceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 市场名称字典
      marketplaceIdOptions: [],
      // 地区名称字典
      regionCodeOptions: [],
      // 使用状态字典
      useStatusOptions: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        marketplaceId: null,
        regionCode: null,
        useStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        marketplaceId: [
          { required: true, message: "市场名称不能为空", trigger: "change" }
        ],
        regionCode: [
          { required: true, message: "地区名称不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("amz_region_code").then(response => {
      this.regionCodeOptions = response.data;
    });
    this.getDicts("amz_marketplace_id").then(response => {
      this.marketplaceIdOptions = response.data;
    });
    this.getDicts("amz_status").then(response => {
      this.useStatusOptions = response.data;
    });

  },
  methods: {
    /** 查询站点维护列表 */
    getList() {
      this.loading = true;
      listMarketplace(this.queryParams).then(response => {
        this.marketplaceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
/*    // 市场名称字典翻译
    marketplaceIdFormat(row, column) {
      return this.selectDictLabel(this.marketplaceIdOptions, row.marketplaceId);
    },*/
    // 地区名称字典翻译
    regionCodeFormat(row, column) {
      return this.selectDictLabel(this.regionCodeOptions, row.regionCode);
    },
    // 使用状态字典翻译
    useStatusFormat(row, column) {
      return this.selectDictLabel(this.useStatusOptions, row.useStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        marketplaceId: null,
        marketplaceName: null,
        marketplaceUri: null,
        countryCode: null,
        regionCode: null,
        regionName: null,
        languageCode: null,
        currencyCode: null,
        currencyRate: null,
        oauthUri: null,
        apiEndpointUri: null,
        lwaEndpointUri: null,
        awsRegion: null,
        useStatus: null,
        createTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加站点维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMarketplace(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改站点维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        let marketplaceIdOptionsChecked = this.marketplaceIdOptions.find((item) => {
          return item.dictValue === this.form.marketplaceId;
        });
        this.form.marketplaceName = marketplaceIdOptionsChecked.dictLabel;

        let regionCodeOptionsChecked = this.regionCodeOptions.find((item) => {
          return item.dictValue === this.form.regionCode;
        });
        this.form.regionName = regionCodeOptionsChecked.dictLabel;

        if (valid) {
          if (this.form.id != null) {
            updateMarketplace(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMarketplace(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除站点维护编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMarketplace(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },

    /**
     *  同步汇率
     */
    handleSyncCurrencyRate(row) {
      this.$confirm('是否确认同步汇率?（涉及频率限制，请勿随意调用！）', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {

          syncCurrencyRate().then(response => {
            this.getList();
            this.msgSuccess(response.msg);
          });

        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有站点维护数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMarketplace(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
